
<template>
  <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">

    <el-form-item label="资讯标题" prop="title">
      <el-input   placeholder="请输入标题" v-model="ruleForm.title"></el-input>
    </el-form-item>

    <el-form-item label="资讯内容" prop="desc">
      <el-input placeholder="请输入正文..." type="textarea" v-model="ruleForm.desc"></el-input>
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="submitForm('ruleForm')">重新提交审核</el-button>
      <el-button type="primary" class="btn" @click="back" round>返回</el-button>
    </el-form-item>

  </el-form>
</template>

<script>
import {ElMessage} from "element-plus";

export default {
  name: 'Edit',
  inject:['reload'],

  data() {
    return {
      ruleForm: {
        title: '',
        desc: ''
      },
      rules: {
        title: [
          { required: true, message: '请输入资讯标题', trigger: 'blur' },
        ],
        desc: [
          { required: true, message: '输入文章内容', trigger: 'blur' }
        ]
      },


    };
  },
  mounted() {
    this.A();
  },

  methods: {

    A(){
      // console.log(this.$route.params.articleId)
      let that = this;
      this.$axios.get('/articles/queryById', {
        params: { "id": this.$route.params.articleId }
      }).then(function (response) {
        // console.log(response)
        let article = response.data;
        that.ruleForm.title = article.articleTitle;
        that.ruleForm.desc = article.articleContent;

      }).catch(function (error) {
        alert(error);
      });
    },

    back(){
      this.$router.go(-1);
      this.reload;
    },

    open() {
      this.$confirm('是否确认重新提交审核?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {

        let flag = true;
        let that = this;
        this.$axios({
          method: "put",
          url:"/upStatus/examine/?articleId=" + this.$route.params.articleId,
        }).then(function (response) {
          // console.log(response);
          if(response.data.code == 2) {
            flag = false;
          }
        }).catch(function (error) {
          console.log(error);
          ElMessage.error(error.message);
        });

        this.$axios({
          method: "post",
          url: "/articles/update",
          data: {
            articleId: that.$route.params.articleId,
            articleTitle:this.$data.ruleForm.title,
            articleContent:this.$data.ruleForm.desc,
          },
        }).then(function (response) {
          // console.log(response);
          if(response.data.code == 2) {
            flag = false;
          }
          if(flag == true) {
            that.$router.push('/list/1');
            that.reload;
            ElMessage.success({
              message: '资讯提交成功!',
              type: 'success'
            });
          }
          else {
            ElMessage.error('资讯提交失败!');
          }

        }).catch(function (error) {
          console.log(error);
          ElMessage.error(error.message);
        });



      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        });
      });
    },

    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.open();
        } else {
          ElMessage.error('error submit!!');
          return false;
        }
      });
    },

  }
}
</script>

<style scoped>

.btn {
  position: relative;
  left: 30%;
  margin-top: 10px;
  width: 100px;
  height: 38px;
}
</style>